package com.redis.springbootredis.rabbitmq.routing;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * @author
 * @create 2020-11-03 19:46
 * 路由模式的生产者
 */
public class RoutingProducer {

    public static void main(String[] args) throws Exception {
        //创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //设置访问的ip地址
        connectionFactory.setHost("192.168.238.132");
        //设置虚拟主机(类似于redis的db)
        connectionFactory.setVirtualHost("ljw");
        //设置密码和账户
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        //新建连接对象
        Connection connection = connectionFactory.newConnection();
        //创建连接信道(需要通信的话必须创建，类似于输送水的水管)
        Channel channel = connection.createChannel();
        //创建生产者
        /*
          String exchange  交换机的名字，路由模式下必填，而且需要指定交换机的类型为直连类型direct
          String routingKey 路由ID，人为设定即可，最好见名知意
          BasicProperties props  附加的消息属性
          byte[] body 消息体，发送的消息内容
        */
        channel.basicPublish("exchagedirect","Emailroutingkey",null,"hello RabbitMQ EmailQueue".getBytes());
        channel.basicPublish("exchagedirect","SMSroutingkey",null,"hello RabbitMQ SMSQueue".getBytes());
        System.out.println("发送成功");
        //生产者发送消息后需要释放资源
        channel.close();  //关闭信道
        connection.close(); //关闭连接
    }
}
